! function(e, $, mui) {

    "use strict";
    var config = {};
    // 定义页面
    var p = function(config) {
        var t = this;
        t.config = config;
        t.userInfo = null;
        t.page = {
            page: 0,
            perpage: 0,
            more: true
        };
        t.element = {
            listContainer: $('#list-container .list'),
            emptyContainer: $('#list-container .empty-list'),
            button_check:$('.button_check'),
            sdate: $('.sdate'),
           // ddate: $('.ddate'),
            searchType: $('.search-type')
        };

        // 初始化
        t.init();

        // 事件监听
        t.listen(11);
    };

    var pt = p.prototype;

    // 页面初始化
    pt.init = function() {
        var t = this;
        t.userInit();
        // 列表加载
          t.element.sdate.val((new Date()).Format('yyyy-MM-dd'));
          // t.element.ddate.val((new Date()).Format('yyyy-MM-dd'));
              t.listInit();
    };

    // 事件初始化
    pt.listen = function(param) {
        var t = this;
        // 刷新列表
        t.element.emptyContainer.on('tap', function() {
            t.listInit();
        });
        
        // 点击查询
        t.element.button_check.on('tap', function() {
            t.listInit();
        });

		// 时间
    	var dtPicker = new mui.DtPicker({type:"date"});	
    	
    	var picker = new mui.PopPicker(); 
    	// choose type
		t.element.searchType.on('tap', function () {
			picker.setData([
				{value:'',text:'全部'},
				{value:'纸浆',text:'纸浆'},
				{value:'板材',text:'板材'},
				{value:'橡塑类',text:'橡塑类'},
				{value:'一般化工品',text:'一般化工品'},
				{value:'农产品',text:'农产品'},
				{value:'其他',text:'其他'},
				]);
			picker.show(function (item) {
				t.element.searchType.html(item[0].text).data('val', item['0'].value);
			});
		});
		
    	// 选择开始时间
/*		t.element.sdate.parent().on('tap', function () {
		    dtPicker.show(function (item) {
			    t.element.sdate.val(item.value);
		    });
		});*/
		
    	// 选择截止时间
		/*t.element.ddate.parent().on('tap', function () {
		    dtPicker.show(function (item) {
			    t.element.ddate.val(item.value);
		    });
		});*/
    };

    // 列表初始化
    pt.listInit = function() {
        var t = this;
        t.element.listContainer.html('');
        t.page = {
            page: 0,
            perpage: 5,
            more: true
        };

        // 列表加载
        t.listLoad();
    };

    // 列表加载
    pt.listLoad = function() {
        var t = this;
        t.page.page++;

        if(t.page.more === false) {
//          mui('#list-container').pullRefresh().endPulldownToRefresh();
            return;
        }
        
       var input_check = $('.input_check').val();
      var sdate = t.element.sdate.val();
      //var ddate = t.element.ddate.val();
      var type = t.element.searchType.data('val') || '';
        
    /*  if(!sdate) {
      	mui.toast('请选择开始时间');
      	return;
      }*/
     /* if(!ddate) {
      	mui.toast('请选择截止时间');
      	return;
      }
*/
        crossDomainAjax({
            url: apiUrl,
            pastDate: {
                "op": "Phone_GetKC_Store",
                "Pid": userInfo.Pid,
                "Code": input_check,
              "sdate": sdate,
              //"ddate": ddate,
              "Type":type
            },
            nativeFlg: false,
            requestType: 'POST'
        }, function(result) {
            if(result.status == 0) {
                mui.toast(result.Message);
                return;
            }
            
            console.log(userInfo.Pid);
            console.log(result);
            var i = 0;
            var firstcount =0;
            var secondcount=0;
            var weight=0;
            var maoweight=0;
            var volume=0;
            var order =0;
            result.Main.forEach(function(item) {
            	firstcount += parseFloat(item.FirstCount);
            	secondcount += parseFloat(item.SecondCount);
            	weight += parseFloat(item.Weight);
            	maoweight += parseFloat(item.MaoWeight);
            	volume += parseFloat(item.Volume);
                    i++
                    order++
                    var firsttime = new Date(item.First_Date).toLocaleDateString();
                    var secondtime = new Date(item.SendDate ).toLocaleDateString();
                    var html = '<tr>';
                    html += '<td>' + order+ '</td>';
                    html += '<td>' + firsttime + '</td>';
                    html += '<td>' + secondtime + '</td>';
                    html += '<td>' + item.TiDanCode + '</td>';
                    html += '<td>' + item.SmName + '</td>';
                    html += '<td>' + item.Spec + '</td>';
                    html += '<td>' + item.PinPai + '</td>';
                    html += '<td>' + item.BaoHao + '</td>';
                    html += '<td>' + item.FirstCount + '</td>';
                    html += '<td>' + item.FirstCountUnit + '</td>';
                    html += '<td>' + item.SecondCount + '</td>';
                    html += '<td>' + item.SecondCountUnit + '</td>';
                    html += '<td>' + item.Weight + '</td>';
                    html += '<td>' + item.WeightUnit + '</td>';
                    html += '<td>' + item.MaoWeight + '</td>';  
                    html += '<td>' + item.Volume + '</td>';
                    html += '<td>' + item.StoreName + '</td>';
                    html += '<td>' + item.HuoWei + '</td>';
                    html += '<td>' + item.RKCode + '</td>';  
                    html += '<td>' + item.WuQuanUint + '</td>';  
                    html += '<td>' + item.UnitName + '</td>';
                    html += '<td>' + item.DengJi + '</td>';
                    html += '<td>' + item.Day + '</td>';
                    html += '<td>' + item.Xiangxing + '</td>';
                    html += '<td>' + item.LiFangDun + '</td>';
                    html += '</tr>';
                    t.element.listContainer.append(html);
            });

			$('.firstcount .num').html(firstcount.toFixed(2));
			$('.secondcount .num').html(secondcount.toFixed(2));
		    $('.weight .num').html(weight.toFixed(4));
		    $('.maoweight .num').html(maoweight.toFixed(4));
		    $('.volume .num').html(volume.toFixed(4));
                    var html = '<tr>';
                    html += '<td>合计</td>';
                    html += '<td></td>';
                    html += '<td></td>';
                    html += '<td></td>';
                    html += '<td></td>';
                    html += '<td></td>';
                    html += '<td></td>';
                    html += '<td></td>';
                    html += '<td></td>';
                    html += '<td>' + firstcount+ '</td>';
                    html += '<td></td>';
                    html += '<td>' + secondcount + '</td>';
                    html += '<td></td>';
                    html += '<td>' + weight + '</td>';
                    html += '<td></td>';
                    html += '<td>' + maoweight + '</td>';
                    html += '<td>' + volume + '</td>';
                    html += '<td></td>';
                    html += '<td></td>';
                    html += '<td></td>';
                    html += '<td></td>';
                    html += '<td></td>';
                    html += '<td></td>';
                    html += '<td></td>';
                    html += '<td></td>';
                    html += '</tr>';
                    //t.element.listContainer.append(html);
                 console.log(JSON.stringify(result));

            // 判断列表是否为空
            if(t.page.page == 1) {
                if(i > 0) {
                    t.element.listContainer.show();
                    t.element.emptyContainer.hide();
                } else {
                	mui.toast('暂无数据');
                    t.element.listContainer.hide();
                    t.element.emptyContainer.show();
                }
            }

//          mui('#list-container').pullRefresh().endPulldownToRefresh();
        });
    };

    // 用户信息初始化
    pt.userInit = function() {
        var t = this;
        t.userInfo = getLocalUserInfo();
    };

    e.page = new p(config);
    mui.init({
      pullRefresh: {
          container: '#list-container',
          down: {
              style: 'circle',
              callback: function() {
                  e.page.listInit();
              } // 下拉刷新
          }
      }
    });

//  e.page.listInit();

}(window, $, mui);